<?php

namespace app\index\dao;

use app\common\model\ConsultingQuestion;
use ShidianUtil\Thinkphp\Dao\DaoTrait;
use think\Collection;

/**
 * ConsultingQuestionDao 数据访问对象
 * Class ConsultingQuestionDao
 * @package app\dao
 * @method ConsultingQuestion findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method ConsultingQuestion insertActive(array $params, array $allow_field = [])
 * @method ConsultingQuestion updateActive(ConsultingQuestion $model, array $params, array $allow_field = [])
 * @method ConsultingQuestion findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method ConsultingQuestion updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(ConsultingQuestion $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class ConsultingQuestionDao
{
    use DaoTrait;

    /**
     * @var ConsultingQuestion
     */
    private $baseModel;

    /**
     * ConsultingQuestionDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = ConsultingQuestion::class;
    }

    public function selectHome($number): Collection
    {
        return ConsultingQuestion::where('isPublish', 1)
            ->with([
                'lawyer' => fn($query) => $query->field(['id', 'nickname', 'headImgUrl', 'cropImgUrl'])
                    ->with([
                        'memberInfo' => fn($subQuery) => $subQuery->field([
                            'memberId',
                            'realname',
                            'provinceId',
                            'cityId',
                        ])->with([
                            'categories' => fn($query) => $query->shortCategory(),
                            'province' => fn($query) => $query->shortArea(),
                            'city' => fn($query) => $query->shortArea(),
                        ])
                    ])
                    ->append(['headImg', 'headImgThumb', 'cropImg'])
                    ->hidden(['headImgUrl']),
            ])
            ->append(['createdStr'])
            ->order("ord", 'ASC')
            ->order('publishTime', 'DESC')
            ->order('id', 'DESC')
            ->limit($number)
            ->select();
    }

    public function selectLawyerQuestion($lawyer, $number): Collection
    {
        return ConsultingQuestion::where('lawyerId', $lawyer)
            ->where('isPublish', 1)
            ->append(['createdStr'])
            ->order("ord", 'ASC')
            ->order('publishTime', 'DESC')
            ->order('id', 'DESC')
            ->limit($number)
            ->select();

    }

    /**
     * 热门咨询-详情
     * @param $id
     * @return ConsultingQuestion|null
     */
    public function findDetail($id): ?ConsultingQuestion
    {
        return ConsultingQuestion::where('id', $id)
            ->with([
                'category' => fn($query) => $query->field(['id', 'name']),
                'lawyer' => fn($query) => $query->field(['id', 'nickname', 'realname', 'headImgUrl', 'cropImgUrl', 'type'])
                    ->with([
                        'memberInfo' => function ($query) {
                            $query->field([
                                'memberId',
                                'commentLevel',
                                'practiceTime',
                            ])->with([
                                'categories' => fn($query) => $query->shortCategory(),
                            ])->append(['practiceTimeStr',]);
                        }
                    ])->append(['headImg', 'headImgThumb', 'cropImg', 'showName'])
                    ->hidden(['headImgUrl']),
            ])
            ->find();
    }

    public function selectRelated($categoryId, array $exclude, $number = 2): Collection
    {
        return ConsultingQuestion::where('categoryId', $categoryId)
            ->when(!empty($exclude), fn($query) => $query->whereNotIn('id', $exclude))
            ->where('isPublish', 1)
            ->with([
                'lawyer' => fn($query) => $query->field(['id', 'nickname', 'realname', 'headImgUrl', 'cropImgUrl', 'type'])
                    ->with([
                        'memberInfo' => function ($query) {
                            $query->field([
                                'memberId',
                                'commentLevel',
                                'practiceTime',
                            ])->append(['practiceTimeStr']);
                        }
                    ])->append(['headImg', 'headImgThumb', 'cropImg', 'showName'])
                    ->hidden(['headImgUrl']),
            ])
            ->orderRand()
            ->limit($number)
            ->select();
    }
}
